<?php

// TODO: review

if (preg_match('/^[http|https]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[#~A-Za-z0-9\.\/%&=\?\-_:\+]+$/i',$_GET['url'])) {

  $urlTarget = urlencode(htmlentities($_GET['url']));
  $url = 'TODO/css-validator/validator?uri=' . $urlTarget . '&profile=css3&usermedium=all&warning=1&lang=en&output=soap12';
  $raw = htmlentities($_GET['raw']);

  $checkUrl = htmlentities($_GET['url']);
  $checkHandle = curl_init($checkUrl);
  curl_setopt($checkHandle,CURLOPT_RETURNTRANSFER,TRUE);
  $checkResponse = curl_exec($checkHandle);
  $checkResponseCode = curl_getinfo($checkHandle,CURLINFO_HTTP_CODE);
  // echo $checkResponseCode;
  if ($checkResponseCode == '404' || $checkResponseCode == '500') {
    echo '?';
    die;
  }

  $buffer = file_get_contents($url);

  $errors = '';
  $posStart = strpos($buffer,'<m:errorcount>');
  $posEnd = strpos($buffer,'</m:errorcount>');
  for ($i=$posStart+strlen($s1); (( $i<($posEnd)) && $i < strlen($buffer)); $i++) {
    $errors .= $buffer[$i];
  }

  // Ahem
  $errorsClean = str_replace('<m:errorcount>', '', $errors);

  if ($raw != 'yes') {
    if ($errorsClean > 9) {
      echo '0';
    } else {
      $errorsCleanTemp = (10 - $errorsClean);
      echo $errorsCleanTemp;
    }
  } else {
    echo $errorsClean;
  }

}

?>